package com.aischool.api;

import java.util.List;

import com.aischool.routes.RouteMapping;
import com.aischool.service.SysAreaService;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

@RouteMapping(url = "/api/area")
public class AreaController extends Controller {
	
	@Inject
	SysAreaService sysAreaService;

	public void getChild() {
		int area_id = getParaToInt("area_id", 0);
		String jsonStr = "{";
		if (area_id > 0) {
			List<Record> list = sysAreaService.getChild(area_id);
			if (area_id == 1) {
				renderJson(list);
				return;
			}
			for(int i=0,l=list.size();i<l;i++) {
				Record rd = list.get(i);
				jsonStr += (i>0?",":"") + "\""+rd.getInt("area_id")+"\":\""+rd.getStr("area_name")+"\"";
			}
		}
		jsonStr += "}";
		renderJson(jsonStr);
	}
	
	public void test() {
		List<Record> listrd = Db.find("SELECT a.* from ai_org a " + 
				"INNER JOIN (SELECT regexp_split_to_table(?, E',') AS ids) AS tt " + 
				"ON CAST(tt.ids AS INTEGER) = a.org_id", "1,2,3");
		//System.err.println(listrd);
		renderJson(listrd);
	}
}
